package com.itetchologyandenglish.home.controller.Api;

import com.github.pagehelper.PageInfo;
import com.itetchologyandenglish.home.config.Log;
import com.itetchologyandenglish.home.enetity.*;
import com.itetchologyandenglish.home.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;
@Api(tags={"用户模块"})
@RestController
@RequestMapping("/admin")
public class AdminApiController {

    @Resource
    AdminService adminService;
    @Resource
    UserAthService userService;
    @Resource
    RoleService roleService;
    @Resource
    AuthService authService;
    @Resource
    PageService pageService;
    @Resource
    SysLogDao sysLogDao;
    @ApiOperation("查看管理员列表")
    @Log("用户维护-查看管理员列表")
    @RequestMapping(value = "/listAdmin", produces = "application/json")
    public Result<List<AuthUser>> listAdmin() {
        List<AuthUser> list = adminService.getList();
        return new Result<List<AuthUser>>(200, "success", list);
    }
    @ApiOperation("删除管理员")
    @Log("用户维护-根据ID删除管理员列表")
    @RequestMapping(value = "/delAdmin", produces = "application/json")
    public Result delAdmin(@ApiParam(name="id",value="管理员Id",required = true) Integer id) {
        if (userService.deactivate(id)) {
            return new Result(200, "停用成功");
        } else {
            return new Result(200, "停用失败");
        }
    }
    @ApiOperation("根据ID搜索管理员")
    @Log("用户维护-根据ID搜索管理员")
    @RequestMapping(value = "/searchAdmin", produces = "application/json")
    public Result<AuthUser> searchAdmin(@ApiParam(name="id",value="管理员Id",required = true)Integer id) {
        AuthUser user = userService.getUserById(id);
        return new Result<AuthUser>(200, "success", user);
    }
    @ApiOperation("更新管理员")
    @Log("用户维护-根据ID等信息更新管理员")
    @RequestMapping(value = "/updateAdmin", produces = "application/json")
    public Result updateAdmin(String username, String password, String tel, Integer delete, String email, Integer id, String roles, String realName) {
        Roles roleUser = roleService.getRolesIdByRoleName(roles);
        AuthUser user = new AuthUser().setUsername(username)
                .setTel(tel).setEmail(email).setPassword(password).setDelete(delete)
                .setId(id).setRoles(roleUser).setRealName(realName);
        System.out.println(user);
        if (userService.update(user) > 0) {
            return new Result(200, "修改成功");
        } else {
            return new Result(200, "修改失败");
        }

    }
    @ApiOperation("添加管理员")
    @Log("用户维护-添加管理员")
    @RequestMapping(value = "/addAdmin", produces = "application/json")
    public Result addAdmin(String username, String password, String tel, String email, Integer id, String roles, String realName) {
        Roles roleUser = roleService.getRolesIdByRoleName(roles);
        System.out.println(roleUser);
        AuthUser user = new AuthUser().setUsername(username)
                .setTel(tel).setEmail(email).setPassword(password)
                .setId(id).setRoles(roleUser).setRealName(realName);
        System.out.println(user);
        if (userService.addUserByAll(user)) {
            return new Result(200, "添加成功");
        } else {
            return new Result(200, "添加失败");
        }
    }
    @ApiOperation("删除管理员")
    @Log("用户维护-删除管理员")
    @RequestMapping(value = "/removeAdmin", produces = "application/json")
    public Result removeAdmin(Integer id) {
        if (userService.removeAdmin(id)) {
            return new Result(200, "删除成功");
        } else {
            return new Result(200, "删除失败");
        }
    }
    @ApiIgnore
    @Log("角色维护-角色列表")
    @RequestMapping(value = "/listRole", produces = "application/json")
    public Result<List<Roles>> listRole() {
        //List<AuthUser> list= adminService.getList();
        List<Roles> roleList = roleService.getRoleList();
        return new Result<List<Roles>>(200, "success", roleList);
    }
    @ApiOperation("模块列表")
    @Log("模块维护-模块列表")
    @RequestMapping(value = "/listAuth", produces = "application/json")
    public Result<List<Auth>> listAuth() {
        List<Auth> authList = authService.list();
        return new Result<List<Auth>>(200, "success", authList);
    }
    @ApiOperation("查找角色")
    @Log("角色维护-根据ID查找角色")
    @RequestMapping(value = "/searchRole", produces = "application/json")
    public Result<List<Auth>> listAuth(Integer id) {
        List<Auth> authList = pageService.getAuthByRoleId(id);
        return new Result<List<Auth>>(200, "success", authList);
    }
    @ApiOperation("删除角色")
    @Log("角色维护-根据ID删除角色")
    @RequestMapping(value = "/deleteRole", produces = "application/json")
    public Result deleteRole(Integer roleId) {
        if (roleService.deleteRole(roleId)) {
            return new Result(200, "删除成功");
        }
        return new Result(200, "删除失败");
    }
    @ApiIgnore
    @Log("用户维护-检查用户名")
    @RequestMapping(value = "/checkAdminName", produces = "application/json")
    public Result<Integer> checkAdminName(String adminName) {
        if (adminService.checkAdminName(adminName) > 0) {
            return new Result<Integer>(200, "error", 0);
        } else {
            return new Result<Integer>(200, "success", 1);
        }
    }

//    @Log("系统操作日志-日志列表")
    @ApiIgnore
    @RequestMapping(value = "/logList", produces = "application/json")
    public Result<PageInfo> logList(@RequestParam("moduleName") String moduleName, @RequestParam("adminName") String adminName, @RequestParam("searchbegin") String searchbegin, @RequestParam("searchend") String searchend, @RequestParam("page") Integer page) {
        System.out.println("哈哈" + moduleName + adminName + searchbegin + searchend + page);
        PageInfo pageInfo = sysLogDao.getAllLogWith(moduleName, adminName, searchbegin, searchend, page);
        return new Result<PageInfo>(200, "success", pageInfo);
    }


}
